//
// Created by 98770 on 2023/6/22.
//
//libCache定义了DNS服务器cache的数据结构和相关操作
#ifndef NETWORK_LIBCACHE_H
#define NETWORK_LIBCACHE_H
#define DEFAULT_CACHE_FILE "..//backup_cache.txt"
#define DEFAULT_TTL 8888


#include "hashmap.h"
#include <stdbool.h>
#include <stdio.h>

#if _WIN32

#include "windows.h"

#elif __linux__

#include <pthread.h>

#endif
typedef struct cacheNode cacheNode;
struct cacheNode {
    char domain[256];
    char ip[256];
    int ttl;
};
//创建hashmap
#if _WIN32
void initMutex();
#endif

bool createHashmap();
//将记录插入hashmap
bool putRecordInHashmap(struct hashmap_s *hashmap, char *domain, char *ip, uint32_t ttl);

bool putRecordInHashmapWithNode(struct hashmap_s *hashmap, cacheNode *node);

//从hashmap中获取记录,返回指向结构体的指针
cacheNode *getRecordFromHashmap(struct hashmap_s *hashmap, char *domain);
//从hashmap中删除记录
bool deleteRecordFromHashmap(struct hashmap_s *hashmap, char *domain);
//从文件中加载记录到hashmap
bool loadCacheFromDisk(struct hashmap_s *hashmap, char *filename);
//将hashmap中的记录存入文件
bool storeCacheToDisk(struct hashmap_s *hashmap);
//销毁hashmap
bool destroyHashmap(struct hashmap_s *hashmap);

//刷新hashmap，将ttl小于0的记录删除，刷新一次TTL减小1
void refreshHashmap(struct hashmap_s *hashmap);

#endif //NETWORK_LIBCACHE_H
